package ttp.service;

import java.util.HashMap;

import ttp.dao.TtpDAO;
import ttp.to.CrushTO;
import ttp.to.TreeTO;
import ttp.utilities.Utilities;

public class CrushService {
	private TtpDAO dao;

	public CrushService(TtpDAO dao) {
		super();
		this.dao = dao;
	}

	public HashMap run() throws Exception{
		long start_time = System.currentTimeMillis();
		CrushTO cto = new CrushTO();
		TreeTO tto = dao.selectTreeByNo(0);
		if(tto!=null){
			int crushNo = dao.sequenceCrush();
			String enterPrise = tto.getEnterPrise();
			String treeType = tto.getTreeType();
			String treeGrade = tto.getTreeGrade();
			int treeCount = tto.getTreeCount();
			int treeWeight = 0;
			if(treeType.equals("소나무")&&treeGrade.equals("A등급")){
				treeWeight = treeCount*3;
			}else if(treeType.equals("소나무")&&treeGrade.equals("B등급")){
				treeWeight = treeCount*2;
			}else if(treeType.equals("소나무")&&treeGrade.equals("C등급")){
				treeWeight = treeCount*1;
			}else if(treeType.equals("은행나무")&&treeGrade.equals("A등급")){
				treeWeight = treeCount*3;
			}else if(treeType.equals("은행나무")&&treeGrade.equals("B등급")){
				treeWeight = treeCount*2;
			}else if(treeType.equals("은행나무")&&treeGrade.equals("C등급")){
				treeWeight = treeCount*1;
			}else if(treeType.equals("오크나무")&&treeGrade.equals("A등급")){
				treeWeight = treeCount*3;
			}else if(treeType.equals("오크나무")&&treeGrade.equals("B등급")){
				treeWeight = treeCount*2;
			}else if(treeType.equals("오크나무")&&treeGrade.equals("C등급")){
				treeWeight = treeCount*1;
			}
			String productDate = Utilities.getNow();
			int crushCheck = (int) (Math.random()*100)+1;
			if(crushCheck==100){
				crushCheck = 1;
			}else{
				crushCheck = 0;
			}
			cto = new CrushTO(crushNo, enterPrise, treeType, treeGrade, treeWeight, productDate, crushCheck, 0, 0);
			dao.insertCrush(cto);
			dao.updateTree(tto.getTreeNo());
			Thread.sleep(3000);
			
		}else if(tto==null){
			Thread.sleep(3000);
			cto = null;
		}
		long end_time = System.currentTimeMillis();
		System.out.println("Crush 처리시간: " + String.format("%,d msec", end_time - start_time));
		HashMap map = new HashMap();
		map.put("cto", cto);
		map.put("time", String.format("%,d msec", end_time - start_time));
		return map;
	}
}
